package com.lqk.demo.concurrencytools.threadpool.executors.scheduledthread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.lqk.demo.n2.util.Sleeper.sleep;

/**
 * @author lqk
 * @date 2022/1/10 15:07
 *
 * newScheduledThreadPool 定时执行任务
 */
@Slf4j
public class TestScheduledExecutorService2 {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
        log.debug("start...");

        pool.scheduleAtFixedRate(()->{
            log.debug("running...");
            sleep(3);   // 会覆盖间隔时间
        },1,1, TimeUnit.SECONDS);

        pool.scheduleWithFixedDelay(()->{
            log.debug("running...");
            sleep(3);   // 不会覆盖间隔时间
        },1,1,TimeUnit.SECONDS);
    }
}
